package com.tanhua.server.controller;

import com.tanhua.model.vo.CountsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.service.TanhuaService;
import com.tanhua.server.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Api(tags = "用户信息相关接口")
@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private TanhuaService tanhuaService;

    @ApiOperation("用户资料 - 读取")
    @GetMapping
    public ResponseEntity getUserInfo(Long userId) {
        UserInfoVo userInfoVo = userInfoService.getById(userId);
        return ResponseEntity.ok(userInfoVo);
    }

    @ApiOperation("用户资料 - 更新头像")
    @PostMapping("/header")
    public ResponseEntity updateHeadPhoto(MultipartFile headPhoto) {
        userInfoService.uploadAvatar(headPhoto);
        return ResponseEntity.ok(null);
    }

    @ApiOperation("用户资料 - 保存")
    @PutMapping
    public ResponseEntity update(@RequestBody UserInfoVo userInfoVo) {
        userInfoService.updateById(userInfoVo);
        return ResponseEntity.ok(null);
    }

    @ApiOperation("相互喜欢，关注，粉丝详情")
    @GetMapping("/friends/{type}")
    public ResponseEntity findFriendsByType(@PathVariable Integer type,
                                            @RequestParam(defaultValue = "1") Long page,
                                            @RequestParam(name = "pagesize", defaultValue = "10") Long pageSize,
                                            String nickname) {
        PageResult result = tanhuaService.findFriendsByType(page, pageSize, nickname, type);
        return ResponseEntity.ok(result);
    }

    @ApiOperation("相互喜欢，关注，粉丝数量")
    @GetMapping("/counts")
    public ResponseEntity getCounts() {
        CountsVo countsVo = tanhuaService.getCounts();
        return ResponseEntity.ok(countsVo);
    }

    @ApiOperation("是否喜欢")
    @GetMapping("/{uid}/alreadyLove")
    public ResponseEntity alreadyLove(@PathVariable Long uid) {
        boolean isLike = tanhuaService.alreadyLove(uid);
        return ResponseEntity.ok(isLike);
    }

    @ApiOperation("取消喜欢")
    @DeleteMapping("/like/{userId}")
    public ResponseEntity deleteLike(@PathVariable Long userId) {
        tanhuaService.deleteLike(userId);
        return ResponseEntity.ok(null);
    }

    @ApiOperation("粉丝喜欢")
    @PostMapping("/fans/{userId}")
    public ResponseEntity fansLike(@PathVariable Long userId) {
        tanhuaService.fansLike(userId);
        return ResponseEntity.ok(null);
    }
}
